<template>
  <div>
    <el-row>此处应该上传组件</el-row>
    <el-upload
      action="#"
      :file-list="fileList"
      list-type="picture-card"
      :class="{disable:fileList.length>0}"
      :on-remove="onRemove"
      :on-change="onChange"
      :before-upload="beforeUpload"
      :http-request="httpRequest"
    >
      <i class="el-icon-plus" />
    </el-upload>
  </div>
</template>

<script>
import COS from 'cos-js-sdk-v5'
const cos = new COS({
  SecretId: 'AKIDWwNr9543vpZ6PULAqjz3tUwMfjXDuRp2',
  SecretKey: '8GF4tc9U2ShjZDi4F7mDdzSmsv8DE5Qm'
})
export default {
  data() {
    return {
      fileList: []
    }
  },
  methods: {
    onRemove(file, newFilelist) {
      this.fileList = newFilelist
    },
    onChange(file, newFilelist) {
      this.fileList = newFilelist
    },
    beforeUpload(file) {
      // 进行类型校验
      const WhiteList = ['image/jpeg', 'image/png']
      if (WhiteList.indexOf(file.type) === -1) {
        this.$message.error('文件类型需要jpeg或png格式')
        return false
      }
      //   进行带下校验,文件显示字节1*1024是1k,181024*1024是1M
      const max = 1 * 1024 * 1024
      if (file.length > max) {
        this.$message.error('文件大小不能超过1M')
        return false
      }
    },
    httpRequest(params) {
      console.log(params)
      cos.putObject({
        Bucket: 'hrtwo-1317163428', /* 填入您自己的存储桶，必须字段 */
        Region: 'ap-nanjing', /* 存储桶所在地域，例如ap-beijing，必须字段 */
        Key: params.file.name, /* 存储在桶里的对象键（例如1.jpg，a/b/test.txt），必须字段 */
        Body: params.file, /* 必须，上传文件对象，可以是input[type="file"]标签选择本地文件后得到的file对象 */
        onProgress: function(progressData) {
          console.log(JSON.stringify(progressData))
        }
      }, (err, data) => {
        console.log(err || data)
        // 上传完毕
        if (!err) {
          this.fileList[0].url = 'http://' + data.Location
          this.fileList[0].status = 'success'
        }
      })
    }
  }

}
</script>

<style lang="scss" scoped >
.disable{
    ::v-deep .el-upload--picture-card{
    display:none;
}

}

</style>
